//
//  HuangwwAuthID+HwwShowAuthID.m
//  Pods
//
//  Created by huangwenwu on 2020/1/14.
//

#import "HuangwwAuthID+HwwShowAuthID.h"
#import "ReactiveObjC.h"
#import "HuangwwFaceAndGestureHeader.h"
#import <FuliBaseLibraryFramework/FuliBaseLibraryFramework.h>
@implementation HuangwwAuthID (HwwShowAuthID)
+ (RACSignal *)validateAuthID{
    return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        [HuangwwAuthID showTouchIdWithTitle:nil falldBackTitle:@"" fallBackBlock:^{
        } resultBlock:^(BOOL useable, BOOL success, NSError * _Nullable error) {
            if (success) {
                kSetUserDefaults(@"open", kAuthIDopen);
                kSetUserDefaults(@(2), kLockType);
                [subscriber sendNext:@(YES)];
                [subscriber sendCompleted];
            }else {
                error.code == LAErrorAuthenticationFailed ? [[self validateAuthID] subscribeNext:^(id  _Nullable x) {}] : @"";
                error.code == LAErrorTouchIDLockout ? [MBProgressHUD showFailureProgress:[NSString stringWithFormat:@"多次错误，%@已被锁定，请到手机解锁界面输入密码",iPhoneX ? @"FaceID" : @"指纹"]] : @"";
                [HuangwwAuthID RemoveAuthID];
                [subscriber sendNext:@(NO)];
                [subscriber sendCompleted];
            }
        }];
        return [RACDisposable disposableWithBlock:^{
            
        }];
    }];
}

+ (RACSignal *)validateAuthIDPay{
    return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        [HuangwwAuthID showTouchIdWithTitle:nil falldBackTitle:@"" fallBackBlock:^{
        } resultBlock:^(BOOL useable, BOOL success, NSError * _Nullable error) {
            if (success) {
                [subscriber sendNext:@"认证成功"];
                [subscriber sendCompleted];
            }else {
                error.code == LAErrorAuthenticationFailed ? [[self validateAuthIDPay] subscribeNext:^(id  _Nullable x) {}] : @"";
                kRemoveUserDefaults(kAuthIDPayopen);
                [subscriber sendError:error];
                [subscriber sendCompleted];
            }
        }];
        return [RACDisposable disposableWithBlock:^{
            
        }];
    }];
}


+ (RACSignal *)validateLoginAuthID{
    return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        [HuangwwAuthID showTouchIdWithTitle:nil falldBackTitle:@"" fallBackBlock:^{
            
        } resultBlock:^(BOOL useable, BOOL success, NSError * _Nullable error) {
            if (success) {
                [subscriber sendNext:@"认证成功"];
                [subscriber sendCompleted];
            }else{
                error.code == LAErrorAuthenticationFailed ? [[self validateLoginAuthID] subscribeNext:^(id  _Nullable x) {}] : @"";
                error.code == LAErrorTouchIDLockout ? ({[HuangwwAuthID RemoveAuthID];}) : @"";
                [subscriber sendError:error];
                [subscriber sendCompleted];
            }
        }];
        return [RACDisposable disposableWithBlock:^{
            
        }];
    }];
}

+ (RACSignal*)UsePayAuthID {
    return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        [HuangwwAuthID showTouchIdWithTitle:nil falldBackTitle:@"输入密码" fallBackBlock:^{
            [subscriber sendNext:RACTuplePack(@(YES),@(NO),nil)];
            [subscriber sendCompleted];
        } resultBlock:^(BOOL useable, BOOL success, NSError * _Nullable error) {
            [subscriber sendNext:RACTuplePack(@(NO),@(success),error)];
            [subscriber sendCompleted];
        }];
        return [RACDisposable disposableWithBlock:^{
            
        }];
    }];
}
@end
